package com.zynote.nfhoujie.dao.impl;

import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import com.zynote.commons.base.BaseDAOHibernate;
import com.zynote.nfhoujie.dao.ICommodityDAO;
import com.zynote.nfhoujie.model.Commodity;
import com.zynote.nfhoujie.model.Favorite;
import com.zynote.nfhoujie.model.SafetyUser;
import com.zynote.nfhoujie.model.view.MultiCommodityView;

/**
 * 
 * @className:CommodityDAOImpl.java
 * @classDescription:图书数据访问层实现类
 * @author: AIDAN SU
 * @createTime: 2012-10-7
 *
 */
@Repository("CommodityDAO")
public class CommodityDAOImpl extends BaseDAOHibernate implements ICommodityDAO {

	@Override
	protected Class<Commodity> getModelClass() {
		return Commodity.class;
	}
	
	@Override
	public Commodity findCommodityById(Long id) {
		return (Commodity)doFindObjectById(id);
	}

	@Override
	public Long createCommodity(Commodity commodity) {
		return (Long)doCreateObject(commodity);
	}

	@Override
	public void updateCommodity(Commodity commodity) {
		doUpdateObject(commodity);
	}
	
	@Override
	public void deleteCommodity(Commodity commodity) {
		doDeleteObject(commodity);
	}
	
	@Override
	public void deleteCommodityById(Long id) {
		doDeleteObject( findCommodityById(id) );
	}

	@Override
	public int countAllCommoditys() {
		int result = 0;
		Session session = super.getSession();
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select count(*) from t_commodity commodity order by commodity.spid desc");			
			Long resultLonger = Long.parseLong(sqlQuery.list().get(0).toString());
			result = resultLonger.intValue();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return result;
	}
	
	@Override
	public int countTypeCommoditys(Integer parentCategory) {
		int result = 0;
		Session session = super.getSession();
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select count(*) from t_commodity commodity where commodity.parent_category=:parentCategory order by commodity.spid desc");			
			sqlQuery.setInteger("parentCategory", parentCategory);	
			Long resultLonger = Long.parseLong(sqlQuery.list().get(0).toString());
			result = resultLonger.intValue();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return result;
	}
	
	@Override
	public int countCommoditys(Long uid) {
		int result = 0;
		Session session = super.getSession();
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select count(*) from t_commodity commodity where commodity.uid=:uid order by commodity.spid desc");		
			sqlQuery.setLong("uid", uid);	
			Long resultLonger = Long.parseLong(sqlQuery.list().get(0).toString());
			result = resultLonger.intValue();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return result;
	}
	
	@Override
	public int countStatusCommoditys(Integer commodityStatus) {
		int result = 0;
		Session session = super.getSession();
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select count(*) from t_commodity commodity where commodity.commodity_status=? order by commodity.spid desc");		
			sqlQuery.setLong(0, commodityStatus);	
			Long resultLonger = Long.parseLong(sqlQuery.list().get(0).toString());
			result = resultLonger.intValue();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MultiCommodityView> listMyCommoditys(int startPosition, int pageSize, Long uid) {
		Session session = super.getSession();
		List<MultiCommodityView>  multiCommoditys = null ;
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select {commodity.*},{user.*},{favorite.*} from t_commodity commodity "+
														"left join t_user  user on commodity.uid=user.uid "+
														"left join t_favorite favorite on commodity.spid=favorite.commodity_id "+
														"where commodity.uid=:uid order by commodity.spid desc");
			sqlQuery.addEntity("commodity",Commodity.class);
			sqlQuery.addEntity("user",SafetyUser.class);
			sqlQuery.addEntity("favorite",Favorite.class);
			multiCommoditys = sqlQuery.setResultTransformer(Transformers.aliasToBean(MultiCommodityView.class))
						.setMaxResults(pageSize)
						.setFirstResult(startPosition)
						.setLong("uid", uid)
						.list();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return multiCommoditys;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MultiCommodityView> listAllCommoditys(int startPosition, int pageSize) {
		Session session = super.getSession();
		List<MultiCommodityView>  multiCommoditys = null ;
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select {commodity.*},{safetyUser.*},{favorite.*} from t_commodity commodity "+
														"left join t_user  safetyUser on commodity.uid=safetyUser.uid "+
														"left join t_favorite favorite on commodity.spid=favorite.commodity_id "+
														"order by commodity.spid desc");
			sqlQuery.addEntity("commodity",Commodity.class);
			sqlQuery.addEntity("safetyUser",SafetyUser.class);
			sqlQuery.addEntity("favorite",Favorite.class);
			multiCommoditys = sqlQuery.setResultTransformer(Transformers.aliasToBean(MultiCommodityView.class))
						.setMaxResults(pageSize)
						.setFirstResult(startPosition)
						.list();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return multiCommoditys;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MultiCommodityView> listTypeCommoditys(int startPosition, int pageSize, int parentCategory) {
		Session session = super.getSession();
		List<MultiCommodityView>  multiCommoditys = null ;
		try{
			SQLQuery sqlQuery = session.createSQLQuery(" select {commodity.*},{safetyUser.*},{favorite.*} from t_commodity commodity "+
														"left join t_user  safetyUser on commodity.uid=safetyUser.uid "+
														"left join t_favorite favorite on commodity.spid=favorite.commodity_id "+
														"where commodity.parent_category=:parentCategory order by commodity.spid desc");
			sqlQuery.setInteger("parentCategory", parentCategory);
			sqlQuery.addEntity("commodity",Commodity.class);
			sqlQuery.addEntity("safetyUser",SafetyUser.class);
			sqlQuery.addEntity("favorite",Favorite.class);
			multiCommoditys = sqlQuery.setResultTransformer(Transformers.aliasToBean(MultiCommodityView.class))
						.setMaxResults(pageSize)
						.setFirstResult(startPosition)
						.list();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			releaseSession(session);
		}
		return multiCommoditys;
	}


}
